package com.jinyi.wwdeng.utils.dialog

import android.content.Context
import android.graphics.Paint
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import com.jinyi.base.utils.HHLog
import com.jinyi.wwdeng.App
import com.jinyi.wwdeng.R
import com.jinyi.wwdeng.databinding.DialogLoginDigitalCodeBinding
import com.jinyi.wwdeng.weight.VerifyEditText

/**
 * Author : wwdeng
 * Date : 2023/8/1 15:44
 * <p>
 * 每天进步一点点
 * <p>
 * Desc : 注册、登录 输入验证码
 */
class LoginDigitalCodeDialog: ProDialog {
    lateinit var binding: DialogLoginDigitalCodeBinding
    private var mCallback: IDialogCallback? = null

    constructor(context: Context?) : super(context) {}
    constructor(context: Context?, callback: IDialogCallback?) : super(context) {
        mCallback = callback
    }

    /**
     * 设置点击回调
     */
    fun setOnClick(callback: IDialogCallback){
        this.mCallback = callback
    }

    override fun getRootView(): View {
        binding = DialogLoginDigitalCodeBinding.inflate(LayoutInflater.from(context))
        return binding.root
    }

    override fun initView() {
        binding.tvResend.paint.flags = Paint.UNDERLINE_TEXT_FLAG//下划线
        binding.tvResend.paint.isAntiAlias = true//抗锯齿
    }

    override fun initListener() {
        setOnClickListener(R.id.ivClose) //关闭
        setOnClickListener(R.id.resendLayout) //重发
        setOnClickListener(R.id.ivRefresh) //刷新
    }

    override fun initData() {
        //输入完成回调
        binding.editCode.addInputCompleteListener(object : VerifyEditText.InputCompleteListener{
            override fun complete(content: String) {
                HHLog.e(">>>>>>>>>>>>>>>$content")
                if (mCallback != null){
                    mCallback!!.inputComplete(content)
                }
            }
        })
    }

    override fun onViewClick(v: View?) {
        when(v!!.id){
            R.id.ivRefresh -> {//刷新
                binding.editCode.clearContent()
            }
        }
        if(mCallback != null){
            mCallback!!.onClick(v)
        }
    }

    //刷新倒计时
    fun onRefreshCountdown(count: Int){
        binding.tvCountdown.text = "$count"
    }

    //显示倒计时
    fun showCountdownLayout(count: Int){
        binding.wrongLayout.visibility = View.GONE
        binding.resendLayout.visibility = View.GONE

        binding.ivRefresh.visibility = View.GONE
        binding.resendNormalLayout.visibility = View.VISIBLE

        onRefreshCountdown(count)
    }

    //显示刷新
    fun showRefreshLayout(){
        binding.wrongLayout.visibility = View.GONE
        binding.resendLayout.visibility = View.GONE

        binding.ivRefresh.visibility = View.VISIBLE
        binding.resendNormalLayout.visibility = View.GONE
    }

    //显示重发
    fun showResendLayout(){
        binding.wrongLayout.visibility = View.GONE
        binding.resendLayout.visibility = View.VISIBLE

        binding.ivRefresh.visibility = View.GONE
        binding.resendNormalLayout.visibility = View.GONE
    }

    //验证码错误
    fun showWrongLayout(){
        binding.wrongLayout.visibility = View.VISIBLE
        binding.resendLayout.visibility = View.VISIBLE

        binding.ivRefresh.visibility = View.GONE
        binding.resendNormalLayout.visibility = View.GONE


       binding.editCode.setVerifyCodeIsWrong(true)
    }

    interface IDialogCallback {
        //按钮点击
        fun onClick(view: View?)
        //输入完成
        fun inputComplete(content: String)
    }
}